<?php

class Mobitv_Cronjobs_Alert
{
    protected $_reportDao;
    protected $_config;
    protected $_date;
    
    public function __construct()
    {
        $this->_reportDao = Vega_Dao::factory('Mobitv_Admin_Report');
        $this->_config = Vega_App::getInstance()->coreConfig;
        $this->_date = date('Y-m-d');
    }

    public function smsStatistic()
    {
        $fromDate = $toDate = $yesterday = date('Y-m-d', time() - 60*60*24);
        $data = $this->_reportDao->getGeneralStatistic($fromDate, $toDate);

        $subscriptionCount = json_decode($data[0]['tv_subscription_count'], true);
        $unsubscriptionCount = json_decode($data[0]['tv_unsubscription_count'], true);
        $vodRevenue = $data[0]['vod_revenue'];
        $tvRevenue = $data[0]['tv_revenue'];

        $data = "vod=$vodRevenue&tv=$tvRevenue";
        return $this->pushMessage(1, $data);
    }

    function pushMessage($serviceId, $content) {
        $curl = curl_init('http://222.255.28.107:8668/push');
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 15);
        curl_setopt($curl, CURLOPT_TIMEOUT, 45);

        curl_setopt($curl, CURLOPT_VERBOSE, FALSE);
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

        curl_setopt($curl, CURLOPT_POST, TRUE);
        $data = 'date=' . date('Y-m-d H:i:s') . '&serviceId=' . $serviceId . '&' .$content;
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

        $response = curl_exec($curl);


        if (curl_errno($curl)) {
                return false;
        }
        curl_close($curl);

        $obj = json_decode($response);
        if (!$obj) return false;
        $errorCode = $obj->errorCode;
        return ($errorCode == 0);
    }
}

